/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVASE Song Huongkang PROJECT
 * All Rights Reserved.
 */

package 练习;

import org.junit.Test;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Description
 * @Author 俊昭
 * @Date 2022/4/25
 */
public class BufferedDemo {
    /**
     * 利用缓冲流进行文件加密
     */
    @Test
    public void mission002() {
        // 造文件
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            File f1 = new File("C:\\Users\\Junzhao\\IOStream\\Exer\\加密后.png");
            File f2 = new File("C:\\Users\\Junzhao\\IOStream\\Exer\\再解密.png");

            // 造流
            FileInputStream fis = new FileInputStream(f1);
            FileOutputStream fos = new FileOutputStream(f2);

            // 造缓冲流
            bis = new BufferedInputStream(fis);
            bos = new BufferedOutputStream(fos);

            // 具体加密细节
            int b;
            while ((b = bis.read()) != - 1) {
                bos.write(b ^ 5);// 异或运算 位运算符 换为二进制 比较每一位 相同就是0 否则1
                // 例如 15^7=8  15:1111
                //              7:0111
                //                1000:8

//                bos.flush();刷新此缓冲的输出流。这将强制将任何缓冲的输出字节写入基础输出流
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            // 关闭流 要求是先关外层再关内层 但是内层不需要手动关
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 利用缓冲流进行文件中字符出现次数的统计并写入到文件中
     */
    @Test
    public void mission003() {
        // 造文件
        BufferedReader br = null;
        Map<String, Integer> map = new HashMap<>();

        try {
            FileReader reader = new FileReader("C:\\Users\\Junzhao\\IOStream\\Exer\\示例文本.txt");

            br = new BufferedReader(reader);
            int value;

            int i;
            String s;
            while ((i = br.read()) != - 1) {

                s = "  " + (char) i;// 加一个空格方便在文件中查看

                // key 存在将值加一 否则直接存入
                if (map.containsKey(s)) {
                    value = map.get(s) + 1;
                    map.put(s, value);

                } else {
                    map.put(s, 1);

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
//            System.out.println(map);

//            将结果存入文件
            BufferedWriter bw = null;
            try {
                FileWriter writer = new FileWriter("C:\\Users\\Junzhao\\IOStream\\Exer\\示例文本之统计结果.txt");

                bw = new BufferedWriter(writer);

                Set<Map.Entry<String, Integer>> set = map.entrySet();// 这里使用数集可以进行排序 但没必要

                char[] c;
                int len;
                // 将Set
                for (Map.Entry<String, Integer> entry : set) {
                    c = entry.toString().toCharArray();// 简单的转换
                    len = c.length;
                    bw.write(c, 0, len);
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (br != null) {
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
